package com.rykj.energy.database.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.rykj.energy.common.utils.BeanUtils;
import com.rykj.energy.common.utils.CollectionUtils;
import com.rykj.energy.database.mapper.NoticeMapper;
import com.rykj.energy.database.pojo.BasicNotice;
import com.rykj.energy.database.service.IDataDictService;
import com.rykj.energy.database.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("noticeService")
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private IDataDictService dataDictService;



    /**
     * 保存通知方式
     */
    @Override
    public void saveNotice(BasicNotice basicNotice) throws Exception {
        BasicNotice old = noticeMapper.selectById(basicNotice.getId());
        BeanUtils.copyProperties(basicNotice, old);
        noticeMapper.insert(old);
    }

    /**
     * 查询通知方式列表
     */
    @Override
    public List<BasicNotice> getList(){
        List<BasicNotice> notices = new ArrayList<>();
        String sql ="" ;

        List<Map<String,Object>> list = noticeMapper.getNoticeList();
        if(list!=null && list.size()>0){
            for (Map<String,Object> map:list){
                JSONObject jsonObj =new JSONObject(map);
                BasicNotice notice = JSONObject.parseObject(jsonObj.toString(), BasicNotice.class);
                notices.add(notice);
            }
        }
        return CollectionUtils.senseEmpty(notices);
    }

    @Override
    public BasicNotice findOneByBusinessType(String businessType){
        return noticeMapper.findOneByBusinessType(businessType);
    }
}
